//引入express模块
const express = require('express');
//使用url模块封装路径
const url = require('url');
//使用qs模块封装参数
const qs = require('qs');
//获取post请求数据的body模块
const bodyParser=require('body-parser');

//创建express实例
const app = express();

//使用body
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

//解决跨域
app.use("*",(req,res,next)=>{
    //设置头信息
    res.setHeader("Access-Control-Allow-Origin","*");
    res.setHeader("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
    res.setHeader("Access-Control-Allow-Headers", "X-Requested-With,Content-Type");
    //放行
    next();
});

//设置请求路径
app.get('/result', (request, response) => {
    //request能获取请求中的参数
    //response响应结果
    response.send('Hello World!')
})

app.get('/getData', (request, response) => {
    //request能获取请求中的参数
    let param = qs.parse(url.parse(request.url).query);
    console.log(param);

    //response响应结果
    response.send({"stuNo":"1001","stuName":"zhangsan"})
})
app.post('/getData', (request, response) => {
    //request能获取请求中的参数
    let jg = request.body;
    console.log(jg);

    //response响应结果
    response.send({"stuNo":"1001","stuName":"zhangsan"})
})

app.get('/searchInfo', (request, response) => {
    //request能获取请求中的参数
    console.log(request.url)
    console.log(url.parse(request.url).query);
    console.log(qs.parse( url.parse(request.url).query ));
    //response响应结果
    response.send({"stuNo":"1001","stuName":"zhangsan"})
})


//设置服务器的ip、端口等信息
const port = 3300
const hostName = '127.0.0.1'
app.listen(port, hostName,() => {
    console.log(`Express for Node  http://${hostName}:${port}`)
})

